import type { VbenFormSchema } from '#/adapter/form';

import { isUndefined } from '@vben/utils';

import { getLineListApi } from '#/api/master/line';
import { getFinishedMaterialListApi } from '#/api/master/material';

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      dependencies: {
        triggerFields: ['routingCode'],
        disabled(values) {
          return !isUndefined(values.id);
        },
      },
      fieldName: 'routingCode',
      label: '路线编号',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'routingName',
      label: '路线名称',
      rules: 'required',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        showSearch: true,
        optionFilterProp: 'label',
        labelField: 'lineName',
        valueField: 'id',
        api: getLineListApi,
      },
      fieldName: 'lineId',
      label: '产线',
      rules: 'required',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        showSearch: true,
        optionFilterProp: 'label',
        labelField: 'name',
        valueField: 'id',
        api: getFinishedMaterialListApi,
      },
      fieldName: 'productId',
      label: '产品',
      rules: 'required',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
      },
      fieldName: 'isMaster',
      label: '主路线',
      rules: 'required',
      defaultValue: true,
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 1,
        precision: 0,
      },
      fieldName: 'order',
      label: '排序',
      defaultValue: 100,
      rules: 'required',
    },
    {
      component: 'Switch',
      componentProps: {
        checkedValue: 1,
        unCheckedValue: 0,
        checkedChildren: '启用',
        unCheckedChildren: '禁用',
        class: 'w-auto',
      },
      fieldName: 'status',
      label: '状态',
      defaultValue: 1,
      rules: 'required',
    },
    {
      component: 'Textarea',
      componentProps: {
        showCount: true,
        maxlength: 128,
      },
      fieldName: 'remark',
      label: '备注',
    },
  ];
}

/**
 * 获取复制表单的字段配置。
 */
export function useCopySchema(): VbenFormSchema[] {
  return [
    {
      component: 'ApiSelect',
      componentProps: {
        labelField: 'lineName',
        valueField: 'id',
        api: getLineListApi,
      },
      disabled: true,
      fieldName: 'lineId',
      label: '产线',
      rules: 'required',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        labelField: 'name',
        valueField: 'id',
        api: getFinishedMaterialListApi,
      },
      fieldName: 'productId',
      label: '产品（新）',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'routingCode',
      label: '路线编号（新）',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'routingName',
      label: '路线名称（新）',
      rules: 'required',
    },
  ];
}
